package com.bonc;

import com.bonc.web.cors.CorsFilter;
import com.bonc.web.filter.LoginFilter;
import com.bonc.web.xss.XssFilter;
import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 过滤器配置类
 * @Author: ChenZhiXiang
 * @CreateDate: 2018/12/19 0019 15:06
 * @Version: 1.0
 */
@Configuration
public class WebConfiguation {
    @Value("${csrfUrl}")
    private String csrfUrl ;
    @Value("${loginHtml}")
    private String loginHtml ;
    @Value("${corsFlag}")
    private Boolean corsFlag;
    @Value("${hostValue}")
    private String hostValue ;

    /**
     * 注册跨域过滤器
     * @return
     */
    public FilterRegistrationBean corsFilterRegistrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CorsFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("corsFlag",String.valueOf(corsFlag));
        registrationBean.setName("CorsFilter");
        //执行顺序，越小越先执行
        registrationBean.setOrder(3);
        return registrationBean;
    }

    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

/*    @Bean
    public FilterRegistrationBean testFilterRegistrationBean() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("csrfUrl", csrfUrl);
        registration.addInitParameter("loginHtml", loginHtml);
        registration.addInitParameter("corsFlag", String.valueOf(corsFlag));
        registration.addInitParameter("hostValue", hostValue);
        registration.setName("LoginFilter");
        //执行顺序，order越小越先执行
        registration.setOrder(2);
        return registration;
    }*/

    /**
     * 注册xss的过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns("/*");
        registration.setName("XssFilter");
        //执行顺序，order越小越先执行
        registration.setOrder(1);
        return registration;
    }

}
